package com.bankcomm;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKEvent;
import com.baidu.mapapi.MKGeneralListener;
import com.bankcomm.api.impl.BankcommGet2ApiImpl;
import com.bankcomm.net.http.IXAsyncImageLoader;
import com.rytong.tools.ui.BaseView;
import com.xioai.framework.XIAOIApplication;

/**
 * <pre>
 * 此类的作用主要是实现应用程序集的全局变量， 
 * 也就是任何一个Activity中都可以访问的变量，
 * 它不会因为Activity的生命周期结束而消失。
 * </pre>
 */
public class BankcommApplication extends XIAOIApplication {
	public static String TAG = "bankcomm";

	public static Context mContext;
	public static IXAsyncImageLoader mImageLoader;
	private static BankcommApplication instance = new BankcommApplication();

	//保存初始化的Activity
	private List<Activity> activityList = BaseView.activityList;
	public static BankcommApplication getInstance() {
		return instance;
	}


	// 百度MapApi的管理类
	public BMapManager mBMapMan = null;

	// 授权Key
	public String mStrKey = "ABEB648DD71DE9286C1AA327CDE33CF2D22D7277";

	public static BankcommGet2ApiImpl server;
	
	public LocationClient mLocationClient = null;

	public Handler mHandler = null;

	// 常用事件监听，用来处理通常的网络错误，授权验证错误等
	public static class MyGeneralListener implements MKGeneralListener {

		@Override
		public void onGetNetworkState(int iError) {
			// Toast.makeText(BankcommApplication.getInstance().getApplicationContext(), "您的网络出错啦！", Toast.LENGTH_LONG).show();
		}

		@Override
		public void onGetPermissionState(int iError) {
			if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
				// 授权Key错误：
//				Toast.makeText(BankcommApplication.getInstance().getApplicationContext(), "授权Key不正确！", Toast.LENGTH_LONG).show();
			}
		}
	}

	@Override
	public void onCreate() {
		mLocationClient = new LocationClient( this );
		super.onCreate();

		mContext = this.getApplicationContext();
		mImageLoader = new IXAsyncImageLoader();
		instance = this;
		server = new BankcommGet2ApiImpl();
		mBMapMan = new BMapManager(this);
		mBMapMan.init(this.mStrKey, new MyGeneralListener());

	}

	// 建议在您app的退出之前调用destroy()函数，避免重复初始化带来的时间消耗
	@Override
	public void onTerminate() {
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		super.onTerminate();
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
	}
	
	/**
	 * 将Activity添加到activityList中
	 * @param activity
	 */
	public void addActivity(Activity activity){
		activityList.add(activity);
	}
	
	/**
	 * 将Activity移除
	 * @param activity
	 */
	public void removeActivity(Activity activity){
		if(null != activity && activityList.contains(activity)){
			activityList.remove(activity);			
		}
	}
	
	/**
	 * 退出所有的Activity
	 */
	public void exit(){
		for(Activity activity:activityList){
			if(null != activity){				
				activity.finish();
			}
		}
	}
}
